package cn.beetech.demo.digest;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AesUtils {

    /**
     * 参数分别代表 算法名称/加密模式/数据填充方式
     */
    private static final String ALGORITHM_ECB = "AES/ECB/PKCS5Padding";


    /**
     * 加密算法
     */
    public static final String KEY_ALGORITHM = "AES";

    /**
     * 加密
     *
     * @param content    加密的字符串
     * @param encryptKey key值
     * @return String
     * @throws Exception 加密异常
     */
    public static String encrypt(String content, String encryptKey) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(encryptKey.getBytes(StandardCharsets.UTF_8), KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM_ECB);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
        // 采用base64算法进行转码,避免出现中文乱码
        return Base64.encodeBase64String(b);
    }


    /**
     * 加密
     *
     * @param content    加密的字符串
     * @param encryptKey key值
     * @return byte[]
     * @throws Exception 加密异常
     */
    public static byte[] encryptByte(String content, String encryptKey) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(encryptKey.getBytes(StandardCharsets.UTF_8), KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM_ECB);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 解密
     *
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     * @return String
     * @throws Exception
     */
    public static String decrypt(String encryptStr, String decryptKey) throws Exception {
        return decrypt(ALGORITHM_ECB, encryptStr, decryptKey);
    }


    /**
     * 解密
     *
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     * @return String
     * @throws Exception
     */
    public static String decrypt(String paddingModel, String encryptStr, String decryptKey) throws Exception {
        Cipher cipher = Cipher.getInstance(paddingModel);
        SecretKeySpec keySpec = new SecretKeySpec(decryptKey.getBytes(StandardCharsets.UTF_8), KEY_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        // 采用base64算法进行转码,避免出现中文乱码
        byte[] encryptBytes = Base64.decodeBase64(encryptStr);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes, StandardCharsets.UTF_8);
    }


    /**
     * 解密
     *
     * @param encryptBytes 解密的字符串
     * @param decryptKey   解密的key值
     * @return String
     * @throws Exception
     */
    public static byte[] decrypt(byte[] encryptBytes, String decryptKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_ECB);
        SecretKeySpec keySpec = new SecretKeySpec(decryptKey.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        // 采用base64算法进行转码,避免出现中文乱码
        return cipher.doFinal(encryptBytes);
    }
}
